package com.jonsion.web.controller;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jonsion.domain.Account;
import com.jonsion.exception.CustomException;
import com.jonsion.service.IAccountService;

@Controller
@RequestMapping("/account")
public class AccountController {
	protected static final Logger logger = LoggerFactory
			.getLogger(AccountController.class);

	@Autowired
	private IAccountService accountService;

	/**
	 * 转向用户添加页面
	 * 
	 * @return 返回转向信息
	 */
	@RequestMapping("/regist")
	public String regist() {
		return "/account/regist";
	}

	/**
	 * 添加用户
	 * 
	 * @param acc实体
	 * @return 返回转向信息
	 */
	@RequestMapping("/go2regist")
	public String go2regist(Account acc) {
		try {
			accountService.regist(acc);
			logger.debug("add success");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("增加时发生异常：", e);
		}
		return "redirect:/";
	}

	@RequestMapping("/login")
	public String login(Account acc,HttpSession session) {
		boolean flg = false;
		try {
			flg = accountService.login(acc);
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String username="";
		
		if (flg) {
			username=acc.getUserName();
		}
		
		session.setAttribute("username", username);
		
		return "index";
	}

	// /**
	// * 查询用户
	// * @param acc实体
	// * @return 返回转向信息
	// */
	// @RequestMapping("/queryPage")
	// public String queryPage(Account acc,Model model) {
	// try {
	// int count =accountMapper.getCount(acc);
	// Pagenation pagenation=new Pagenation(PAGE_SIZE,acc.getPageNum(),count);
	// acc.setStartRow(pagenation.getStartRow());
	// acc.setPageSize(PAGE_SIZE);
	// List<Account> list=accountMapper.getAllList(acc);
	// pagenation.setList(list);
	// model.addAttribute("pagenation",pagenation);
	// } catch (Exception e) {
	// e.printStackTrace();
	// //logger.error("query时发生异常：",e);
	// }
	// return "userList";
	// }
}
